package cn.mily.org.service.impl;

import cn.mily.basics.query.BaseQuery;
import cn.mily.basics.service.IBasicService;
import cn.mily.basics.service.impl.BasicServiceImpl;
import cn.mily.org.domain.Department;
import cn.mily.org.domain.Employee;
import cn.mily.org.mapper.DepartmentMapper;
import cn.mily.org.query.DepartmentQuery;
import cn.mily.org.service.IDepartmentService;
import cn.mily.basics.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BasicServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper mapper;

    @Override
    public List<Employee> getMangerName() {
        /*查询领导名*/
        return mapper.getMangerName();
    }

    /*获取子级部门*/
    private void findChildByid(List<Department> parentNames,Long cid){
        /*判断子级部门是否为空*/
        if(parentNames.size()>0){
            /*遍历子级部门*/
            for (Department d:parentNames) {
                /*根据父id找到孩子*/
                List<Department> childs = mapper.findChildByid(d.getId(),cid);
                /*判断子级是否为空*/
                d.setChildren(childs.size()==0?null:childs);
                /*找子部门的子部门   递归*/
                findChildByid(childs,cid);
            }
        }
    }

    @Override/*获取部门级联*/
    public List<Department> findParentName(Long id) {
        /*获取一二级部门关联*/
        List<Department> parentName = mapper.findParentName(id);
        ArrayList<Department> list = new ArrayList<>();
        for (Department department : parentName) {
            List<Department> children = department.getChildren();
            for (Department child : children) {
                if(!child.getId().equals(id)){
                    list.add(child);
                }
            }
            department.setChildren(list.size()==0?null:list);
        }
        findChildByid(parentName,id);
        return parentName;
    }
}
